package cn.edu.suda.catalina.health;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import java.util.HashMap;
import java.util.Map;

import cn.edu.suda.catalina.util.SharedPreferencesUtil;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FiveFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FiveFragment extends Fragment {

    private Map<String, Integer> map;
    private EditText weight;
    private EditText height;

    public FiveFragment() {
    }

    public static FiveFragment newInstance() {
        return new FiveFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_five, container, false);

        map = new HashMap<>();

        weight = (EditText) rootView.findViewById(R.id.five_weight);
        height = (EditText) rootView.findViewById(R.id.five_height);

        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        float result = (float) 0.0;
        if (!TextUtils.isEmpty(height.getText()) && Float.valueOf(height.getText().toString()) > 0) {
            result = Float.valueOf(weight.getText().toString()) / (Float.valueOf(height.getText().toString()) * Float.valueOf(height.getText().toString()));
        }
        if (result < 24) {
            map.put("X5", 0);
        } else if (result >= 28) {
            map.put("X5", 2);
        } else {
            map.put("X5", 1);
        }
        SharedPreferencesUtil.editSharedPreferences(getContext(), map);
    }

}
